package com.carson.undergo.ui.activity

import android.view.View
import androidx.fragment.app.Fragment
import com.carson.undergo.R
import com.carson.undergo.databinding.ActivityMainBinding
import com.carson.undergo.ui.fragment.HomeFragment
import com.carson.undergo.ui.fragment.MineFragment
import com.model.common.base.activity.BaseActivity
import com.model.common.utils.always.ToastUtils
import com.model.common.utils.function.version.CheckVersionUtils
import com.model.common.utils.function.version.VersionUtils

/**
 * Create by Carson on 2021/12/22.
 * MainActivity --- 程序主界面
 */
class MainActivity : BaseActivity<ActivityMainBinding>() {

    private var exitTime: Long = 0
    private lateinit var currentFragment: Fragment
    private lateinit var homeFragment: HomeFragment
    private lateinit var mineFragment: MineFragment

    override fun getViewBinding(): ActivityMainBinding =
        ActivityMainBinding.inflate(layoutInflater)

    override fun initViewsAndEvents() {
        //检查更新
        checkVersion()
        addFragment()
    }

    private fun checkVersion() {
        val desInfo = ArrayList<String>()
        desInfo.add("修复部分已知问题")
        desInfo.add("优化部分UI")
        val weChatUrl =
            "https://gdown.baidu.com/data/wisegame/4c625516bd55f355/ff1f4c625516bd55f355200829321579.apk"
        //比较新版本和旧版本，检查是有需要更新
        val isUpdate = VersionUtils.compareVersions("1.0.0.5", getVersionName(mContext))
        if (isUpdate) {
            val utils = CheckVersionUtils(mContext, weChatUrl, desInfo, "1.0.0.5")
            utils.showUpdateVersion()
        }
    }

    private fun addFragment() {
        binding.tvHome.isSelected = true
        homeFragment = HomeFragment()
        mineFragment = MineFragment()
        currentFragment = homeFragment

        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.frame_layout, currentFragment)
        fragmentTransaction.commit()
    }


    override fun addListener() {
        binding.tvHome.setOnClickListener(this)
        binding.tvMine.setOnClickListener(this)
    }

    override fun onWidgetClick(view: View?) {
        binding.tvHome.isSelected = false
        binding.tvMine.isSelected = false
        when (view?.id) {
            //首页
            R.id.tv_home -> {
                binding.tvHome.isSelected = true
                showFragment(homeFragment)
            }
            //我的
            R.id.tv_mine -> {
                binding.tvMine.isSelected = true
                showFragment(mineFragment)
            }
        }
    }

    /**
     * fragment跳转判断
     */
    private fun showFragment(toFragment: Fragment) {
        if (currentFragment != toFragment) {
            val transition = supportFragmentManager.beginTransaction()
            if (!toFragment.isAdded) {
                transition.hide(currentFragment).add(R.id.frame_layout, toFragment)
                    .commitAllowingStateLoss()
            } else {
                transition.hide(currentFragment).show(toFragment).commitAllowingStateLoss()
            }
            currentFragment = toFragment
        }
    }

    override fun onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            exitTime = System.currentTimeMillis()
            ToastUtils.showMessage(mContext, "再按一次退出程序")
        } else {
            super.onBackPressed()
        }
    }
}